package sgi.client;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.Timer;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JSlider;

public class SGIImageSequenceViewer extends JFrame {

	private JPanel contentPane;
	private JLabel lblImage;
	private JScrollPane scrollPane;
	private boolean RepeatButtonPressed;
	private List<ImageIcon> ImageList;
	private JButton btnClose;
	public static SGIImageSequenceViewer sequenceViewer;
	private Timer timer;
	private Iterator<ImageIcon> it;
	private int interval;
	private JSlider slider;

	public SGIImageSequenceViewer(List<ImageIcon> ImageList) {
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 748, 577);
		contentPane = new JPanel();
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		this.ImageList = ImageList;
		setRepeatButtonPressed(false);
		it = ImageList.iterator();
		scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 11, 712, 470);
		contentPane.add(scrollPane);
		
		lblImage = new JLabel("");
		scrollPane.setViewportView(lblImage);
		
		JButton btnRepeat = new JButton("Repeat");
		btnRepeat.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(isRepeatButtonPressed() == true)  
				{
					setRepeatButtonPressed(false);
				}
				else
				{
					setRepeatButtonPressed(true);
					timer.setRepeats(true);
					timer.start();
				}
				
			}
		});
		btnRepeat.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnRepeat.setForeground(Color.BLUE);
		btnRepeat.setBounds(588, 492, 89, 38);
		contentPane.add(btnRepeat);
		
		btnClose = new JButton("Close");
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SGIImageSequenceViewer.sequenceViewer.close();
			}
		});
		btnClose.setForeground(Color.BLUE);
		btnClose.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnClose.setBounds(65, 492, 89, 38);
		contentPane.add(btnClose);
		
		slider = new JSlider(JSlider.HORIZONTAL,50,2000,1025);
		slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
            	timer.setDelay(slider.getValue());
            }
        });
		slider.setBounds(243, 492, 296, 23);
		contentPane.add(slider);
		
		JLabel lblSlower = new JLabel("Slower");
		lblSlower.setBounds(505, 516, 73, 14);
		contentPane.add(lblSlower);
		
		JLabel lblFaster = new JLabel("Faster");
		lblFaster.setBounds(243, 516, 58, 14);
		contentPane.add(lblFaster);
		
		timer = new Timer(slider.getValue(), new ActionListener() {
		    public void actionPerformed(ActionEvent evt) {
		    
		    try {
		    	if(it.hasNext() == true)
			    {
			    	lblImage.setIcon(it.next());
			    	lblImage.setHorizontalAlignment(JLabel.CENTER);
			    	lblImage.setVerticalAlignment(JLabel.CENTER);
			    }
		    	else
		    		if(isRepeatButtonPressed() == true)  
		    			it = SGIImageSequenceViewer.sequenceViewer.ImageList.iterator();
		    		else
		    		{
		    			timer.setRepeats(false);
		    			timer.stop();
		    		}
			  

		    } catch (NullPointerException Ne){};}
		});

		timer.start();
		
	}
	
	protected void close() {
		
		SGIImageSequenceViewer.sequenceViewer.setVisible(false);
		SGIImageSequenceViewer.sequenceViewer=null;
		
	}


	public boolean isRepeatButtonPressed() {
		return RepeatButtonPressed;
	}

	public void setRepeatButtonPressed(boolean repeatButtonPressed) {
		RepeatButtonPressed = repeatButtonPressed;
	}
}
